package com.wutuo.roster.provider.constant;

import cn.apestar.core.framework.exception.BusinessException;
import cn.apestar.core.utils.DateEnum;
import cn.apestar.core.utils.DateUtils;
import cn.apestar.util.CommonUtil;
import com.wutuo.roster.provider.enums.GradeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * Created by admin on 16-5-14.
 */
public class GradeUtil {

    public static String[] grades = new String[] {"P1", "P2", "P3", "P4", "P5", "P6"};

    /**
     * 根据年级获取对应的年份
     *
     * @param grade P1,P2,P3,P4,P5,P6,OTH
     * @return year
     */
    public static long getYearByGrade(String grade) {

        if (!containGrade(grade)) {
            throw new BusinessException(500, "expire.fail.grade.is.invalid");
        }
        GradeEnum gradeEnum = GradeEnum.valueOf(grade);
        String monthStart = "-09-01";
        Long year = Long.parseLong(DateUtils.formatDate(new Date(), DateEnum.YYYY));
        Date date = new Date();
        Date dateS = DateUtils.parseDate(year + monthStart);
        if (date.getTime() < dateS.getTime()) {
            year = year - 1;
        }
        return year - gradeEnum.getStep();
    }

    public static List getGradeList() {

        Map mapYear = new SMap(0, "一", 1, "二", 2, "三", 3, "四", 4, "五", 5, "六");
        String monthStart = "-09-01";
        Integer year = Integer.parseInt(DateUtils.formatDate(new Date(), DateEnum.YYYY));
        List list = new ArrayList();
        Date date = new Date();
        Date dateS = DateUtils.parseDate(year + monthStart);
        if (date.getTime() < dateS.getTime()) {
            year = year - 1;
        }
        for (int i = 0; i < 6; i++) {
            Map map = new HashMap();
            map.put("year", year - i);
            map.put("grade", mapYear.get(String.valueOf(i)).toString() + "年级");
            list.add(map);
        }
        return list;
    }

    public static Long getGradeNow() {

        Map map = (Map) getGradeList().get(0);
        return CommonUtil.toLong(map.get("year"), 0l);
    }

    private static boolean containGrade(String grade) {

        for (String item : grades) {
            if (StringUtils.equals(item, grade)) {
                return true;
            }
        }
        return false;
    }
}
